package com.open.web.rest;

import com.open.basic.BaseResponse;
import com.open.basic.constant.BizConstant;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;


/**
 * @author: codex
 * @Date: 2024/4/21 15:46
 * @Version: V1.0
 * @description: desc:requestID控制器
 */

@Slf4j
@RestController
public class RequestController {

    private static final String CHAT_ARRAY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    private static final String NUMBER_ARRAY = "0123456789";

    private static final Long MIN = 1000000000L;

    private static final Long MAX = 9999999999L;

    private static final AtomicLong ATOMIC_LONG = new AtomicLong(MIN);

    /**
     * ID 生成器
     *
     * @return
     */
    @GetMapping("id")
    public BaseResponse<String> getId() {
        try {
            TimeUnit.MILLISECONDS.sleep(50);
        } catch (InterruptedException e) {
            log.error("getId sleep error：", e);
        }
        return BaseResponse.success(UUID.randomUUID().toString().replace("-", ""));
    }

    /**
     * ID 生成器
     *
     * @return
     */
    @GetMapping("int/id")
    public BaseResponse<Long> getIntId() {
        Long andAdd = null;
        try {
            andAdd = ATOMIC_LONG.getAndAdd(1);
            if (andAdd > MAX) {
                andAdd = MIN;
                ATOMIC_LONG.set(andAdd);
            }

            TimeUnit.MILLISECONDS.sleep(50);
        } catch (InterruptedException e) {
            log.error("getId sleep error：", e);
        }
        return BaseResponse.success(andAdd);
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public BaseResponse<String> index() {
        return BaseResponse.success(BizConstant.BIZ_SUCCESS.getCode());
    }
}
